//    Copyright (C) Kherty.  All rights reserved.
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using OpenLS.Spreadsheet.UI.Internals;
using OpenLS.Spreadsheet.UI.Internals.Resources;
using OpenLS.UI;

namespace OpenLS.Spreadsheet.UI
{
    partial class SortStateResources
    {
        public SortStateResources()
        {
            InitializeComponent();
        }
        void OnAddLevel(object sender, RoutedEventArgs args)
        {
            SortState sortState = GetSortState(sender);
            sortState.SortConditions.Add(new SortCondition(sortState));
        }

        private static SortState GetSortState(object sender)
        {
            var f = (FrameworkElement)sender;
            return f.DataContext as SortState;
        }
        void OnDeleteLevel(object sender, RoutedEventArgs args)
        {
            SortState sortState = GetSortState(sender);
            var b = (Button)sender;
            var lv = (Selector)b.Tag;
            if (lv.SelectedIndex == -1)
                return;
            sortState.SortConditions.RemoveAt(lv.SelectedIndex);
        }
        void OnCopyLevel(object sender, RoutedEventArgs args)
        {
            SortState sortState = GetSortState(sender);
            var b = (Button)sender;
            var lv = (Selector)b.Tag;
            if (lv.SelectedIndex == -1)
                return;
            SortCondition c = sortState.SortConditions[lv.SelectedIndex];
            SortCondition c2 = c.Clone();
            sortState.SortConditions.Add(c2);
        }
        void OnOptions(object sender, RoutedEventArgs args)
        {
            var d = new CustomSortOptionsControl();
            SortState ss = GetSortState(sender);
            var o = new CustomSortOptions(ss);
            d.DataContext = o;
            WindowServices.ShowDialog(
                d, UIStringTable.SortOptions, 
                sender as UIElement,
                delegate
                    {
                        ss.IsCaseSensitive = o.IsCaseSensitive;
                        if (ss.SortByColumn != o.SortByColumn)
                        {
                            ss.SortByColumn = o.SortByColumn;
                            ss.SortConditions.Clear();
                            ss.SortConditions.Add(new SortCondition(ss));
                        }
                    });
        }
        class CustomSortOptions
        {
            public CustomSortOptions(SortState ss)
            {
                IsCaseSensitive = ss.IsCaseSensitive;
                SortByColumn = ss.SortByColumn;
            }

            public bool IsCaseSensitive { get; set; }

            public bool SortByColumn { get; set; }
        }
    }
}
